//
//  UITextView+ADUtil.m
//  ADKit
//
//  Created by duzexu on 2017/12/1.
//  Copyright © 2017年 duzexu. All rights reserved.
//

#import "UITextView+ADUtil.h"
#import "ADKitHeaders.h"

static char AD_TextView_Placeholder_Key;

@implementation UITextView (ADUtil)
@dynamic placeholderLabel,placeholder;

- (void)ad_limitMaxLength:(NSInteger)maxLength {
    @weakify(self);
    [[[RACSignal merge:@[RACObserve(self, text),self.rac_textSignal]] skip:0] subscribeNext:^(NSString * text) {
        @strongify(self);
        UITextRange *selectedRange = [self markedTextRange];
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制,防止中文被截断
        if (position) return ;
        if (text.length <= maxLength) return;
        //中文和emoj表情存在问题，需要对此进行处理
        NSRange range;
        NSUInteger inputLength = 0;
        for(int i =0 ; i < text.length && inputLength <= maxLength; i += range.length){
            range = [self.text rangeOfComposedCharacterSequenceAtIndex:i];
            inputLength += [text substringWithRange:range].length;
            if (inputLength > maxLength) self.text = [text substringWithRange:NSMakeRange(0, range.location)];
        }
    }];
}

- (void)setPlaceholder:(NSAttributedString *)placeholder {
    [self setAssociateValue:placeholder withKey:&AD_TextView_Placeholder_Key];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidChangeNotification
                                                  object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextViewTextDidChangeNotification object:self];
    if (self.placeholderLabel) {
        [self.placeholderLabel removeFromSuperview];
    }

    self.placeholderLabel = [[UILabel alloc] init];
    self.placeholderLabel.attributedText = placeholder;
    self.placeholderLabel.numberOfLines = 0;
    self.placeholderLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self addSubview:self.placeholderLabel];
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(self.contentInset.left+4);
        make.top.equalTo(self).offset(self.contentInset.top+8);
        make.width.equalTo(self).offset(-self.contentInset.right-self.contentInset.right-4);
        make.bottom.greaterThanOrEqualTo(self).offset(-self.contentInset.bottom);
    }];
    self.placeholderLabel.hidden = self.text.length > 0 ? YES : NO;
}

- (NSAttributedString *)placeholder {
    return [self getAssociatedValueForKey:&AD_TextView_Placeholder_Key];
}

#pragma mark -- NSNotification

- (void)textFieldChanged:(NSNotification *)notification {
    if (self.placeholder) {
        self.placeholderLabel.hidden = YES;
        if (self.text.length == 0) {
            self.placeholderLabel.hidden = NO;
        }
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidChangeNotification
                                                  object:self];
}

@end

@implementation UITextView (ADLocalized)

- (void)setLocalized_text:(NSString *)localized_text {
    self.text = localized_text.ad_localized;
}

@end
